package gprc.mydemo;

import io.grpc.BindableService;
import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.util.concurrent.TimeUnit;

public class GrpcServer {

    private Server server;

    private void start() throws Exception{
        BindableService bindableService;
        this.server = ServerBuilder.forPort(8899).addService(new StudentServiceImpl()).build().start();

        System.out.println("服务器启动成功！");

        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            System.out.println("关闭jvm");
            GrpcServer.this.stop();
        }));

        System.out.println("服务器启动成功后");
    }

    private void stop(){
        if (null!=this.server){
            this.server.shutdown();
        }
    }

    private void awitTermination() throws Exception{
        if (null!=this.server){
//            this.server.awaitTermination(3000, TimeUnit.MILLISECONDS);
            this.server.awaitTermination();
        }
    }

    public static void main(String[] args) throws Exception {
        GrpcServer server = new GrpcServer();

        server.start();
        server.awitTermination();
    }
}
